﻿namespace SecurityAdministration
{
    using System;
    using System.Collections.Generic;
    using System.Web.UI.WebControls;

    using Microsoft.SharePoint;

    public partial class ListDisplayAdministrationUserControl : System.Web.UI.UserControl
    {
        #region Methods

        protected void addField_Click( object sender, EventArgs e )
        {
            if ( columnSelectionDepartmentList.SelectedIndex == -1 )
                return;

            if ( departmentListControl.SelectedIndex == -1 )
                return;
            if ( availableFieldsControl.SelectedIndex == -1 )
                return;

            TargetFieldsDataCollectionBL fieldProcessor = new TargetFieldsDataCollectionBL( );
            string siteURL = SPContext.Current.Web.Url;

            while ( availableFieldsControl.SelectedItem != null )
            {
                currentlyVisibleFields.Items.Add( availableFieldsControl.SelectedItem.Text );
                fieldProcessor.SaveSelectedField( siteURL, departmentListControl.SelectedItem.Text, columnSelectionDepartmentList.SelectedItem.Text, columnSelectionDepartmentList.SelectedItem.Value, availableFieldsControl.SelectedItem.Text );
                availableFieldsControl.Items.Remove( availableFieldsControl.SelectedItem );
            }
            List<DepartmentVisibleFields> allFieldsForSource = new List<DepartmentVisibleFields>( );
            TargetFieldsDataCollectionBL allFieldsProcessing = new TargetFieldsDataCollectionBL( );

            string sourceDepartment = departmentListControl.SelectedItem.Text.Trim( );

            allFieldsProcessing.GetAllDepartmentSelectedFields( siteURL, sourceDepartment );

            allFieldsForSource = allFieldsProcessing.AvailableFields;
            PopulateGeneralViewTreeViewControl( allFieldsForSource );
        }

        protected void Page_Load( object sender, EventArgs e )
        {
            if ( !this.IsPostBack )
            {
                DepartmentInformationBL departments = new DepartmentInformationBL( );
                List<DepartmentInformation> departmentListInformation = new List<DepartmentInformation>( );
                List<DepartmentInformation> fixedDataListInformation = new List<DepartmentInformation>( );

                string siteURL = SPContext.Current.Web.Url;

                departments.LoadDepartments( siteURL, false );

                departmentListInformation = departments.DepartmentList;

                departments.LoadDepartments( siteURL, true );
                fixedDataListInformation = departments.FixedDepartmentList;

                columnSelectionDepartmentList.DataSource = fixedDataListInformation;
                columnSelectionDepartmentList.DataTextField = "DepartmentName";
                columnSelectionDepartmentList.DataValueField = "ListName";
                columnSelectionDepartmentList.DataBind( );

                departmentListControl.DataSource = departmentListInformation;
                departmentListControl.DataTextField = "DepartmentName";
                departmentListControl.DataValueField = "ListName";
                departmentListControl.DataBind( );
            }
        }

        protected void removeField_Click( object sender, EventArgs e )
        {
            if ( columnSelectionDepartmentList.SelectedIndex == -1 )
                return;

            if ( departmentListControl.SelectedIndex == -1 )
                return;
            if ( currentlyVisibleFields.SelectedIndex == -1 )
                return;

            TargetFieldsDataCollectionBL fieldProcessor = new TargetFieldsDataCollectionBL( );
            string siteURL = SPContext.Current.Web.Url;

            while ( currentlyVisibleFields.SelectedItem != null )
            {
                availableFieldsControl.Items.Add( currentlyVisibleFields.SelectedItem.Text );
                fieldProcessor.DeleteSelectedField( siteURL, departmentListControl.SelectedItem.Text, columnSelectionDepartmentList.SelectedItem.Text, currentlyVisibleFields.SelectedItem.Text );
                currentlyVisibleFields.Items.Remove( currentlyVisibleFields.SelectedItem );
            }
            List<DepartmentVisibleFields> allFieldsForSource = new List<DepartmentVisibleFields>( );
            TargetFieldsDataCollectionBL allFieldsProcessing = new TargetFieldsDataCollectionBL( );

            string sourceDepartment = departmentListControl.SelectedItem.Text.Trim( );

            allFieldsProcessing.GetAllDepartmentSelectedFields( siteURL, sourceDepartment );

            allFieldsForSource = allFieldsProcessing.AvailableFields;
            PopulateGeneralViewTreeViewControl( allFieldsForSource );
        }

        protected void SelectionDepartmentChanged( object sender, EventArgs e )
        {
            List<DepartmentVisibleFields> allFieldsForSource = new List<DepartmentVisibleFields>( );
            TargetFieldsDataCollectionBL allFieldsProcessing = new TargetFieldsDataCollectionBL( );

            if ( columnSelectionDepartmentList.SelectedIndex == -1 )
                return;

            if ( departmentListControl.SelectedIndex == -1 )
                return;

            if ( columnSelectionDepartmentList.SelectedItem.Text.Trim( ).Equals( departmentListControl.SelectedItem.Text.Trim( ) ) )
            {
                availableFieldsControl.Items.Clear( );
                availableFieldsControl.Enabled = false;
                currentlyVisibleFields.Enabled = false;
                return;
            }
            availableFieldsControl.Enabled = true;
            currentlyVisibleFields.Enabled = true;

            List<DepartmentFields> departmentFieldInformation = new List<DepartmentFields>( );
            DepartmentInformationBL departments = new DepartmentInformationBL( );

            string siteURL = SPContext.Current.Web.Url;

            availableFieldsControl.Items.Clear( );

            string targetDepartment = columnSelectionDepartmentList.SelectedItem.Text.Trim( );
            departments.GetNonDepartmentFields( siteURL, columnSelectionDepartmentList.SelectedItem.Text.Trim( ), departmentListControl.SelectedItem.Text.Trim( ) );
            departmentFieldInformation = departments.AvailableFields;
            availableFieldsControl.DataSource = departmentFieldInformation;
            availableFieldsControl.DataTextField = "FieldName";
            availableFieldsControl.DataValueField = "InternalFieldName";
            availableFieldsControl.DataBind( );

            allFieldsProcessing.GetSourceTargetDepartmentSelectedFields( siteURL, departmentListControl.SelectedItem.Text.Trim( ), targetDepartment );

            allFieldsForSource = allFieldsProcessing.AvailableFields;

            List<DepartmentVisibleFields> filteredFieldResults = allFieldsForSource.FindAll( delegate( DepartmentVisibleFields deptFields )
            {
                if ( deptFields.TargetDepartmentName.Trim( ).Equals( targetDepartment ) )
                    return true;
                else
                    return false;
            } );

            currentlyVisibleFields.Items.Clear( );
            currentlyVisibleFields.DataSource = filteredFieldResults;
            currentlyVisibleFields.DataValueField = "TargetFieldName";
            currentlyVisibleFields.DataTextField = "TargetFieldName";
            currentlyVisibleFields.DataBind( );
        }

        protected void SourceDepartmentChanged( object sender, EventArgs e )
        {
            List<DepartmentVisibleFields> allFieldsForSource = new List<DepartmentVisibleFields>( );
            TargetFieldsDataCollectionBL allFieldsProcessing = new TargetFieldsDataCollectionBL( );

            string siteURL = SPContext.Current.Web.Url;
            if ( departmentListControl.SelectedIndex == -1 )
                return;
            else
            {
                string sourceDepartment = departmentListControl.SelectedItem.Text.Trim( );

                allFieldsProcessing.GetAllDepartmentSelectedFields( siteURL, sourceDepartment );

                allFieldsForSource = allFieldsProcessing.AvailableFields;
                PopulateGeneralViewTreeViewControl( allFieldsForSource );
            }

            if ( columnSelectionDepartmentList.SelectedIndex == -1 )
                return;

            string targetDepartment = columnSelectionDepartmentList.SelectedItem.Text.Trim( );

            List<DepartmentVisibleFields> filteredFieldResults = allFieldsForSource.FindAll( delegate( DepartmentVisibleFields deptFields )
            {
                if ( deptFields.TargetDepartmentName.Trim( ).Equals( targetDepartment ) )
                    return true;
                else
                    return false;
            } );

            currentlyVisibleFields.Items.Clear( );
            currentlyVisibleFields.DataSource = filteredFieldResults;
            currentlyVisibleFields.DataValueField = "TargetFieldName";
            currentlyVisibleFields.DataTextField = "TargetFieldName";
            currentlyVisibleFields.DataBind( );
        }

        private void PopulateGeneralViewTreeViewControl( List<DepartmentVisibleFields> allFieldsForSource )
        {
            TreeNode rootNode = new TreeNode( "Fields Selected for: " + departmentListControl.SelectedItem.Text.Trim( ), "0" );
            TreeNode departmentNode = null;
            TreeNode fieldNode = null;
            generalAllFieldsView.Nodes.Clear( );
            string currentDepartment = "";

            foreach ( DepartmentVisibleFields dept in allFieldsForSource )
            {
                if ( !currentDepartment.Equals( dept.TargetDepartmentName.Trim( ) ) )
                {
                    currentDepartment = dept.TargetDepartmentName.Trim( );

                    departmentNode = new TreeNode( currentDepartment );

                    rootNode.ChildNodes.Add( departmentNode );
                }
                fieldNode = new TreeNode( dept.TargetFieldName );
                departmentNode.ChildNodes.Add( fieldNode );
            }
            rootNode.Expand( );
            generalAllFieldsView.Nodes.Add( rootNode );
        }

        #endregion Methods
    }
}